package com.machine.service.order.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.machine.bean.Componentpart;
import com.machine.bean.PageBean;
import com.machine.bean.PriceOrder;
import com.machine.dao.order.PriceOrderDao;
import com.machine.service.order.PriceOrderServer;

@Component(value = "priceOrderServer")
public class PriceOrderServerImpl implements PriceOrderServer {
	
	private PriceOrderDao priceOrderDao;
	
	

	public PriceOrderDao getPriceOrderDao() {
		return priceOrderDao;
	}
	@Resource
	public void setPriceOrderDao(PriceOrderDao priceOrderDao) {
		this.priceOrderDao = priceOrderDao;
	}

	@Override
	public int add(PriceOrder priceOrder) {
		return priceOrderDao.add(priceOrder);
	}

	@Override
	public int delete(PriceOrder priceOrder) {
		return priceOrderDao.delete(priceOrder);
	}

	@Override
	public int deleteByIdList(List<Integer> list) {
		return priceOrderDao.deleteByIdList(list);
	}

	@Override
	public PriceOrder find(PriceOrder priceOrder) {
		return priceOrderDao.find(priceOrder);
	}

	@Override
	public PageBean findAll(String providerName,String buyName,int page, int length) {
		
		String hql= "from PriceOrder priceOrder where priceOrder.providerName = '" + providerName + "' OR priceOrder.buyName= '"+buyName+"'";
		int allRow = priceOrderDao.getAllRowCount(hql); // 总记录数
		int totalPage = PageBean.countTotalPage(length, allRow); // 总页数
		int offset = PageBean.countOffset(length, page); // 当前页开始记录
		//int length = length; // 每页记录数
		int currentPage = PageBean.countCurrentPage(page);
		List<PriceOrder> list = priceOrderDao.findAll( providerName, buyName, offset, length);
		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(length);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();
		System.out.println("allrow : " + allRow);
		System.out.println("totalPage : " + totalPage);
		System.out.println("offset : " + offset);
		System.out.println("currentPage : " + currentPage);
		return pageBean;
		
	//	return priceOrderDao.findAll( providerName, buyName, offset, length);
	}

	@Override
	public PriceOrder findById(Integer id) {
		return priceOrderDao.findById(id);
	}

	@Override
	public int update(PriceOrder priceOrder) {
		return priceOrderDao.update(priceOrder);
	}

}
